﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
//Own libs
using GBG_DBM.Support.DAL;
using GBG_DBM.GlobalParameters;
using GBG_DBM.UI;

namespace GBG_DBM.UI
{
    public partial class login_UI : Form
    {

        #region glass panel test

        //[StructLayout(LayoutKind.Sequential)]
        //public struct MARGINS
        //{
        //    public int Left;
        //    public int Right;
        //    public int Top;
        //    public int Bottom;
        //}

        //[DllImport("dwmapi.dll", PreserveSig = false)]
        //public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

        //[DllImport("dwmapi.dll", PreserveSig = false)]
        //public static extern bool DwmIsCompositionEnabled();

        //protected override void OnLoad(EventArgs e)
        //{
        //    if (DwmIsCompositionEnabled())
        //    {
        //        MARGINS margin = new MARGINS();
        //        margin.Right = margin.Left = margin.Bottom = margin.Top = -1;
        //        DwmExtendFrameIntoClientArea(this.Handle, ref margin);
        //    }
        //    base.OnLoad(e);
        //}

        //protected override void OnPaintBackground(PaintEventArgs e)
        //{
        //    base.OnPaintBackground(e);
        //    if (DwmIsCompositionEnabled())
        //    {
        //        e.Graphics.Clear(Color.Black);
        //    }
        //}

        #endregion


        #region FIELDS

        //set username as a fixed variable
        //because Access database is using "Admin" as the defult user
        private string _userName = "Admin";
        private string _password;
        private string _connString;

        private OleDbConnection _dbConnection = new OleDbConnection();

        #endregion

        public login_UI()
        {
            InitializeComponent();

            this.AcceptButton = login_Login_UI;
            this.CancelButton = cancel_Login_UI;

            username_text_Login_UI.Enabled = false;
            username_text_Login_UI.Text = _userName;

            this.login_Login_UI.Enabled = false;

            //keyboard events
            pw_text_Login_UI.KeyUp += new KeyEventHandler(pw_text_Login_UI_KeyEvent);
        }

        private void pw_text_Login_UI_KeyEvent(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (pw_text_Login_UI.Text != string.Empty)
            {
                this.login_Login_UI.Enabled = true;
            }
            else
            {
                this.login_Login_UI.Enabled = false;
            }
        }

        private void cancel_Login_UI_Click(object sender, EventArgs e)
        {
            Application.Exit();
            //System.Environment.Exit(0);
        }

        private void login_Login_UI_Click(object sender, EventArgs e)
        {
            //set password as a global variable
            _password = pw_text_Login_UI.Text;
            DBHelper.SetPassword(_password);

            _connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + GlobalParams.DBPath + ";Jet OLeDb:DataBase Password=" + _password;
            //_connString = DBHelper.ConnectionString;
            _dbConnection = new OleDbConnection(_connString);
            try
            {
                _dbConnection.Open();
            }
            catch (OleDbException _OleErr)
            {
                MessageBox.Show("Login fail! \nPlease check your login details!" + "\nReason: " + _OleErr.Message.ToString(), "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1
                    );
                pw_text_Login_UI.Text = "";
            }

            if (_dbConnection.State == ConnectionState.Open)
            {
                GlobalParameters.GlobalParams.IsLogined = true;
                //update connection string in setting.cs
                GBG_DBM.Properties.Settings.Default.DatabaseConnectionString = _connString;
                _dbConnection.Close();
                
                //if login success then run Main UI and close the login UI to release system resource
                Program.form = new Main_UI();
                this.Close();
            }

            //for testing
            //MessageBox.Show("username:" + _userName + "\npassword:" + DBHelper.SetPassword + "\nConnString" + _connString, "Infomation",
            //    MessageBoxButtons.OK,
            //    MessageBoxIcon.Information);
        }


        //If user enter username and pasword, enable login button
        //private void check_username_pw()
        //{
        //    if (username_text_Login_UI.Text.Trim() != String.Empty && pw_text_Login_UI.Text.Trim() != String.Empty)
        //    {
        //        login_Login_UI.Enabled = true;
        //    }
        //    else login_Login_UI.Enabled = false;
        //}
    }
}
